﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using YunCMS.Common;
using YunCMS.BLL;
using YunCMS.Model;
using YunCMS.Controls.Page;

namespace YunCMS.Web.Admin.User
{
    public partial class EditRole : System.Web.UI.Page
    {
        private YCUserRoleBLL _bll = null;

        protected YCUserRoleBLL BLL
        {
            get
            {
                if (_bll == null)
                {
                    _bll = new YCUserRoleBLL();
                }
                return _bll;
            }
        }


        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindRpt(rptList1);
                string action = Request.QueryString["action"];
                int id;
                if (!string.IsNullOrEmpty(action) && action == "edit")
                {
                    string _id = Request.QueryString["id"];

                    if (!int.TryParse(_id, out id))
                        id = 0;

                    ViewState["ID"] = id;
                    BindControls(id);

                    saveBt.Text = "更新";
                }
                else
                {
                    saveBt.Text = "新增";
                }
            }
        }

        private void BindRpt(Repeater rptList1)
        {
            rptList1.DataSource = BLL.GetActionList();
            rptList1.DataBind();
        }

        private void BindControls(int id)
        {
            if (BLL.IsExist(id))
            {
                YCUserRole model = BLL.GetYCUserRoleWithActions(id);
                FillControls(model);
            }
            else
            {
                JScript.Alert("不存在该条记录", this);
            }
        }

        private void FillControls(YCUserRole model)
        {
            txtRoleName.Text = model.Name;
            ddlRoleType.SelectedValue = model.Type.ToString();
            //基本设置
            for (int i = 0; i < rptList1.Items.Count; i++)
            {
                HiddenField hidID = (HiddenField)rptList1.Items[i].FindControl("hidId");
                HtmlInputCheckBox hcbNavValue =(HtmlInputCheckBox)rptList1.Items[i].FindControl("cblNavName");
                string navValue = hcbNavValue.Value;
                if (model.Actions != null)
                {
                    int id = int.Parse(hidID.Value.Trim());
                    YCUserRoleActions modelt = (model.Actions as List<YCUserRoleActions>).Find(
                        p => p.ActionID == id);
                    if (modelt != null)
                    {
                        hcbNavValue.Checked = true;
                    }
                }
            }
        }

        private void FillModel(YCUserRole model)
        {
            model.Name = txtRoleName.Text.Trim();
            model.Type = int.Parse(ddlRoleType.SelectedValue);

            List<YCUserRoleActions> ls = new List<YCUserRoleActions>();
            //基本设置
            for (int i = 0; i < rptList1.Items.Count; i++)
            {
                HiddenField hidID = rptList1.Items[i].FindControl("hidId") as HiddenField;
                HtmlInputCheckBox hcbNavValue = rptList1.Items[i].FindControl("cblNavName") as HtmlInputCheckBox;
                if (hidID != null && hcbNavValue != null)
                {
                    int id = int.Parse(hidID.Value.Trim());
                    if (hcbNavValue.Checked == true)
                    {
                        ls.Add(new Model.YCUserRoleActions { ActionID = id, RoleID = model.ID });
                    }
                }
            }

            model.Actions = ls;
        }

        private int Add()
        {
            int i = 0;
            YCUserRole model = new YCUserRole();
            FillModel(model);
            i = BLL.AddWithActions(model);
            return i;
        }

        private int Update()
        {
            int i = 0, id;
            if (ViewState["ID"] != null)
            {
                id = System.Convert.ToInt32(ViewState["ID"]);
                YCUserRole model = BLL.GetYCUserRole(id);
                FillModel(model);
                i = BLL.UpdateWithActions(model);
            }
            return i;
        }

        protected void saveBt_Click(object sender, EventArgs e)
        {
            int i = 0;
            string action = Request.QueryString["action"];
            if (!string.IsNullOrEmpty(action) && action == "edit")
            {
                i = Update();
            }
            else
            {
                i = Add();
            }

            if (i > 0)
            {
                Response.Redirect("RoleList.aspx");
            }
        }
    }
}